$(document).ready(function() {
	google.load("maps", "3.5", {other_params:"sensor=false",
		   callback: loadMap});
	$(document).keydown( function(e){
	if ( e.keyCode == 13 || e.keyCode == 3 ) {
	    $("#getDirections").click();
	    e.preventDefault();
	}
    });
});

var GMAP = {};

function loadMap() {
	GMAP.Opt = new google.maps.Marker({
		position: new google.maps.LatLng(41.8276426, -71.3886407),
		title: "OPT"
	});

    var mapOptions = {
    		zoom: 13,
    		center: GMAP.Opt.position,
    		mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    GMAP.map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    GMAP.Opt.setMap(GMAP.map);
    GMAP.directionsService = new google.maps.DirectionsService();
    GMAP.directionsDisplay = new google.maps.DirectionsRenderer();
    GMAP.directionsDisplay.setMap(GMAP.map);
    GMAP.directionsDisplay.setPanel(document.getElementById("directions_panel"));
    $("#getDirections").live("click", fetchDirections);
    $("#startLocation").geo_autocomplete();
};

function fetchDirections() {
    var start = $("#startLocation").val();
    var request = {
	origin: start,
	destination: GMAP.Opt.position,
	travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    GMAP.directionsService.route(request, function(result, status) {
	if (status == google.maps.DirectionsStatus.OK) {
	    GMAP.directionsDisplay.setDirections(result);

	}
    });
}

